home *** CD-ROM | disk | FTP | other *** search
- Path: uunet!husc6!rutgers!aramis.rutgers.edu!dartagnan.rutgers.edu!mcgrew
- From: mcgrew@dartagnan.rutgers.edu (Charles Mcgrew)
- Newsgroups: comp.sources.sun
- Subject: v01i039: Calctool, a calculator for your Sun, Part02/02
- Message-ID: <Jun.29.22.14.29.1989.12588@dartagnan.rutgers.edu>
- Date: 30 Jun 89 02:14:32 GMT
- Organization: Rutgers Univ., New Brunswick, N.J.
- Lines: 1075
- Approved: mcgrew@aramis.rutgers.edu
-
- Submitted-by: chuck@melmac.harris-atd.com (Chuck Musciano)
- Posting-number: Volume 1, Issue 39
- Archive-name: calctool/part02
-
- #! /bin/sh
- # This is a shell archive, meaning:
- # 1. Remove everything above the #! /bin/sh line.
- # 2. Save the resulting text in a file.
- # 3. Execute the file with /bin/sh (not csh) to create the files:
- # keys.h
- # manifest.h
- # memory.c
- # ops.c
- # screen.c
- # This archive created: Thu Jun 29 21:58:43 1989
- export PATH; PATH=/bin:$PATH
- echo shar: extracting "'keys.h'" '(3654 characters)'
- if test -f 'keys.h'
- then
- echo shar: will not over-write existing file "'keys.h'"
- else
- sed 's/^ X//' << \SHAR_EOF > 'keys.h'
- X/************************************************************************/
- X/* Copyright 1988 by Chuck Musciano and Harris Corporation */
- X/* */
- X/* Permission to use, copy, modify, and distribute this software */
- X/* and its documentation for any purpose and without fee is */
- X/* hereby granted, provided that the above copyright notice */
- X/* appear in all copies and that both that copyright notice and */
- X/* this permission notice appear in supporting documentation, and */
- X/* that the name of Chuck Musciano and Harris Corporation not be */
- X/* used in advertising or publicity pertaining to distribution */
- X/* of the software without specific, written prior permission. */
- X/* Chuck Musciano and Harris Corporation make no representations */
- X/* about the suitability of this software for any purpose. It is */
- X/* provided "as is" without express or implied warranty. */
- X/************************************************************************/
- X
- X/************************************************************************/
- X/* */
- X/* Key opcode definitions */
- X/* */
- X/************************************************************************/
- X
- X#define BINARY_OP 0x00010000
- X#define UNARY_OP 0x00020000
- X
- X#define is_binary(x) ((x) & BINARY_OP)
- X#define is_unary(x) ((x) & UNARY_OP)
- X#define is_digit(x) ((x) >= DIGIT_0 && (x) <= DIGIT_EE)
- X#define prec(x) ((x) >> 20)
- X
- X#define NO_OP -1
- X#define DIGIT_0 0
- X#define DIGIT_1 1
- X#define DIGIT_2 2
- X#define DIGIT_3 3
- X#define DIGIT_4 4
- X#define DIGIT_5 5
- X#define DIGIT_6 6
- X#define DIGIT_7 7
- X#define DIGIT_8 8
- X#define DIGIT_9 9
- X#define DIGIT_A 10
- X#define DIGIT_B 11
- X#define DIGIT_C 12
- X#define DIGIT_D 13
- X#define DIGIT_E 14
- X#define DIGIT_F 15
- X#define DIGIT_DOT 16
- X#define DIGIT_CHS 17
- X#define DIGIT_EE 18
- X
- X#define ADD_OP (19 | BINARY_OP | (2 << 20))
- X#define AND_OP (20 | BINARY_OP | (5 << 20))
- X#define CLEAR_OP (21 | UNARY_OP)
- X#define CLOSE_OP (22 | UNARY_OP)
- X#define COS_OP (23 | UNARY_OP)
- X#define DIV_OP (24 | BINARY_OP | (3 << 20))
- X#define EQUAL_OP (25 | UNARY_OP)
- X#define ERASE_OP (26 | UNARY_OP)
- X#define EXC_OP (27 | UNARY_OP)
- X#define EXP_OP (28 | UNARY_OP)
- X#define E_OP (29 | UNARY_OP)
- X#define FACT_OP (30 | UNARY_OP)
- X#define FIX_OP (31 | UNARY_OP)
- X#define ICOS_OP (32 | UNARY_OP)
- X#define INT_OP (33 | UNARY_OP)
- X#define INVEE_OP (34 | UNARY_OP)
- X#define INVERT_OP (35 | UNARY_OP)
- X#define ISIN_OP (36 | UNARY_OP)
- X#define ITAN_OP (37 | UNARY_OP)
- X#define LN_OP (38 | UNARY_OP)
- X#define LOG_OP (39 | UNARY_OP)
- X#define LPAREN_OP (40 | UNARY_OP | (0 << 20))
- X#define LSL_OP (41 | BINARY_OP | (3 << 20))
- X#define MUL_OP (42 | BINARY_OP | (3 << 20))
- X#define NOT_OP (43 | UNARY_OP)
- X#define OR_OP (44 | BINARY_OP | (6 << 20))
- X#define OVER_OP (45 | UNARY_OP)
- X#define PI_OP (46 | UNARY_OP)
- X#define POINT_OP (47 | UNARY_OP)
- X#define POW_OP (48 | UNARY_OP)
- X#define QUIT_OP (49 | UNARY_OP)
- X#define RCL_OP (50 | UNARY_OP)
- X#define ROL_OP (51 | BINARY_OP | (3 << 20))
- X#define ROOT_OP (52 | BINARY_OP | (4 << 20))
- X#define ROR_OP (53 | BINARY_OP | (3 << 20))
- X#define RPAREN_OP (54 | UNARY_OP | (1 << 20))
- X#define RSA_OP (55 | BINARY_OP | (3 << 20))
- X#define RSL_OP (56 | BINARY_OP | (3 << 20))
- X#define SIN_OP (57 | UNARY_OP)
- X#define SQRT_OP (58 | UNARY_OP)
- X#define SQR_OP (59 | UNARY_OP)
- X#define STO_OP (60 | UNARY_OP)
- X#define SUB_OP (61 | BINARY_OP | (2 << 20))
- X#define TAN_OP (62 | UNARY_OP)
- X#define TRUNC_OP (63 | UNARY_OP)
- X#define XOR_OP (64 | BINARY_OP | (6 << 20))
- X#define Y2X_OP (65 | BINARY_OP | (4 << 20))
- SHAR_EOF
- if test 3654 -ne "`wc -c < 'keys.h'`"
- then
- echo shar: error transmitting "'keys.h'" '(should have been 3654 characters)'
- fi
- fi # end of overwriting check
- echo shar: extracting "'manifest.h'" '(1721 characters)'
- if test -f 'manifest.h'
- then
- echo shar: will not over-write existing file "'manifest.h'"
- else
- sed 's/^ X//' << \SHAR_EOF > 'manifest.h'
- X/************************************************************************/
- X/* Copyright 1988 by Chuck Musciano and Harris Corporation */
- X/* */
- X/* Permission to use, copy, modify, and distribute this software */
- X/* and its documentation for any purpose and without fee is */
- X/* hereby granted, provided that the above copyright notice */
- X/* appear in all copies and that both that copyright notice and */
- X/* this permission notice appear in supporting documentation, and */
- X/* that the name of Chuck Musciano and Harris Corporation not be */
- X/* used in advertising or publicity pertaining to distribution */
- X/* of the software without specific, written prior permission. */
- X/* Chuck Musciano and Harris Corporation make no representations */
- X/* about the suitability of this software for any purpose. It is */
- X/* provided "as is" without express or implied warranty. */
- X/************************************************************************/
- X
- X/************************************************************************/
- X/* */
- X/* Manifest constants */
- X/* */
- X/************************************************************************/
- X
- X#define PRIVATE static
- X#define PUBLIC extern
- X
- X#define PI ((double) 3.14159265358979323846)
- X#define E ((double) 2.71828182845904523536)
- X
- X#define SCIENTIFIC 0
- X#define PROGRAMMER 2
- X
- X#define BINARY 2
- X#define OCTAL 8
- X#define DECIMAL 10
- X#define HEXADECIMAL 16
- X
- X#define DEG 0
- X#define RAD 1
- X#define GRAD 2
- X
- X#define MAX_MEMORY 42
- X#define MAX_STACK 100
- X
- X#define index_of(x) ((x == BINARY)? 1 : ((x == OCTAL)? 3 : 4))
- X
- X#ifndef NULL
- X#define NULL 0
- X#endif NULL
- X
- X#ifndef TRUE
- X#define TRUE 1
- X#define FALSE 0
- X#endif TRUE
- SHAR_EOF
- if test 1721 -ne "`wc -c < 'manifest.h'`"
- then
- echo shar: error transmitting "'manifest.h'" '(should have been 1721 characters)'
- fi
- fi # end of overwriting check
- echo shar: extracting "'memory.c'" '(4276 characters)'
- if test -f 'memory.c'
- then
- echo shar: will not over-write existing file "'memory.c'"
- else
- sed 's/^ X//' << \SHAR_EOF > 'memory.c'
- X/************************************************************************/
- X/* Copyright 1988 by Chuck Musciano and Harris Corporation */
- X/* */
- X/* Permission to use, copy, modify, and distribute this software */
- X/* and its documentation for any purpose and without fee is */
- X/* hereby granted, provided that the above copyright notice */
- X/* appear in all copies and that both that copyright notice and */
- X/* this permission notice appear in supporting documentation, and */
- X/* that the name of Chuck Musciano and Harris Corporation not be */
- X/* used in advertising or publicity pertaining to distribution */
- X/* of the software without specific, written prior permission. */
- X/* Chuck Musciano and Harris Corporation make no representations */
- X/* about the suitability of this software for any purpose. It is */
- X/* provided "as is" without express or implied warranty. */
- X/************************************************************************/
- X
- X/************************************************************************/
- X/* */
- X/* Module: memory.c */
- X/* */
- X/* Function: Manage calculator memories */
- X/* */
- X/* Public Names: store_proc store a value */
- X/* recall_proc recall a value */
- X/* exchange_proc exchange a value and a memory */
- X/* do_store store in memory */
- X/* do_recall recall from memory */
- X/* do_exchange exchange with memory */
- X/* */
- X/* Change History: 17 Nov 86 Creation */
- X/* 22 May 87 Added do_* procs */
- X/* */
- X/************************************************************************/
- X
- X#include <suntool/sunview.h>
- X#include <suntool/panel.h>
- X
- X#include "manifest.h"
- X#include "globals.h"
- X
- X#define HAS_STORE 0
- X#define NO_STORE 1
- X
- XPUBLIC Panel keys;
- XPUBLIC struct pixfont *key_font;
- X
- XPRIVATE double memory[MAX_MEMORY] = {0.0};
- XPRIVATE char mem_image[MAX_MEMORY][60];
- XPRIVATE int mem_count = 1;
- XPRIVATE Menu menu = NULL;
- X
- XPRIVATE update_menu(store)
- X
- Xint store;
- X
- X{ int i;
- X
- X if (menu)
- X menu_destroy(menu);
- X menu = menu_create(MENU_INITIAL_SELECTION, MENU_DEFAULT, 0);
- X for (i = 0; i < mem_count; i++) {
- X sprintf(mem_image[i], "%2d: ", i);
- X convert_value(memory[i], &(mem_image[i][4]));
- X menu_set(menu,
- X MENU_ITEM,
- X MENU_STRING, mem_image[i],
- X MENU_FONT, key_font,
- X MENU_VALUE, i + 1,
- X 0,
- X 0);
- X }
- X if (store && mem_count < MAX_MEMORY)
- X menu_set(menu,
- X MENU_ITEM,
- X MENU_STRING, "New Memory",
- X MENU_FONT, key_font,
- X MENU_VALUE, i + 1,
- X 0,
- X 0);
- X menu_set(menu, MENU_DEFAULT, 1, 0);
- X}
- X
- XPUBLIC do_store(slot)
- X
- Xint slot;
- X
- X{
- X memory[slot] = v_stack[v_top];
- X}
- X
- XPUBLIC store_proc(item, event)
- X
- XPanel_item item;
- XEvent *event;
- X
- X{ int slot;
- X
- X if (event_id(event) >= ASCII_FIRST && event_id(event) <= ASCII_LAST)
- X keyboard(event_id(event));
- X else {
- X convert_display();
- X if (event_id(event) == MS_RIGHT) {
- X update_menu(TRUE);
- X if (slot = (int) menu_show(menu, keys, event, 0)) {
- X do_store(--slot);
- X if (slot == mem_count)
- X mem_count++;
- X }
- X }
- X else
- X do_store(0);
- X }
- X}
- X
- XPUBLIC do_recall(slot)
- X
- Xint slot;
- X
- X{
- X v_stack[v_top] = memory[slot];
- X update_display();
- X}
- X
- XPUBLIC recall_proc(item, event)
- X
- XPanel_item item;
- XEvent *event;
- X
- X{ int slot;
- X
- X if (event_id(event) >= ASCII_FIRST && event_id(event) <= ASCII_LAST)
- X keyboard(event_id(event));
- X else {
- X clear_entry();
- X if (event_id(event) == MS_RIGHT) {
- X update_menu(FALSE);
- X if (slot = (int) menu_show(menu, keys, event, 0))
- X do_recall(--slot);
- X }
- X else
- X do_recall(0);
- X }
- X}
- X
- XPUBLIC do_exchange(slot)
- X
- Xint slot;
- X
- X{ double temp;
- X
- X temp = v_stack[v_top];
- X v_stack[v_top] = memory[slot];
- X memory[slot] = temp;
- X update_display();
- X}
- X
- XPUBLIC exchange_proc(item, event)
- X
- XPanel_item item;
- XEvent *event;
- X
- X{ int slot;
- X
- X if (event_id(event) >= ASCII_FIRST && event_id(event) <= ASCII_LAST)
- X keyboard(event_id(event));
- X else {
- X convert_display();
- X if (event_id(event) == MS_RIGHT) {
- X update_menu(FALSE);
- X if (slot = (int) menu_show(menu, keys, event, 0))
- X do_exchange(--slot);
- X }
- X else
- X do_exchange(0);
- X }
- X}
- SHAR_EOF
- if test 4276 -ne "`wc -c < 'memory.c'`"
- then
- echo shar: error transmitting "'memory.c'" '(should have been 4276 characters)'
- fi
- fi # end of overwriting check
- echo shar: extracting "'ops.c'" '(8116 characters)'
- if test -f 'ops.c'
- then
- echo shar: will not over-write existing file "'ops.c'"
- else
- sed 's/^ X//' << \SHAR_EOF > 'ops.c'
- X/************************************************************************/
- X/* Copyright 1988 by Chuck Musciano and Harris Corporation */
- X/* */
- X/* Permission to use, copy, modify, and distribute this software */
- X/* and its documentation for any purpose and without fee is */
- X/* hereby granted, provided that the above copyright notice */
- X/* appear in all copies and that both that copyright notice and */
- X/* this permission notice appear in supporting documentation, and */
- X/* that the name of Chuck Musciano and Harris Corporation not be */
- X/* used in advertising or publicity pertaining to distribution */
- X/* of the software without specific, written prior permission. */
- X/* Chuck Musciano and Harris Corporation make no representations */
- X/* about the suitability of this software for any purpose. It is */
- X/* provided "as is" without express or implied warranty. */
- X/************************************************************************/
- X
- X/************************************************************************/
- X/* */
- X/* Module: ops.c */
- X/* */
- X/* Function: Perform actual calculation */
- X/* */
- X/* Public Names: do_unary handle unary operators */
- X/* do_binary handle binary operators */
- X/* op_str return a operator string */
- X/* */
- X/* Change History: 17 Nov 86 Creation */
- X/* */
- X/************************************************************************/
- X
- X#include <math.h>
- X
- X#include "manifest.h"
- X#include "globals.h"
- X#include "keys.h"
- X
- X#define low_order(b, x) ((((unsigned) 0xffffffff) >> (32 - (b))) & (x))
- X
- XPRIVATE pop_op()
- X
- X{ int i, temp;
- X
- X if (curr_mode != SCIENTIFIC && o_stack[o_top - 1]) {
- X v_stack[v_top] = (double) low_order(curr_width[index_of(curr_base)], (unsigned int) v_stack[v_top]);
- X v_stack[v_top - 1] = (double) low_order(curr_width[index_of(curr_base)], (unsigned int) v_stack[v_top - 1]);
- X }
- X switch (o_stack[--o_top]) {
- X case ADD_OP : v_stack[v_top - 1] += v_stack[v_top];
- X break;
- X case AND_OP : temp = ((unsigned int) v_stack[v_top - 1]) & ((unsigned int) v_stack[v_top]);
- X v_stack[v_top - 1] = (double) temp;
- X break;
- X case DIV_OP : v_stack[v_top - 1] /= v_stack[v_top];
- X break;
- X case LPAREN_OP : return;
- X break;
- X case LSL_OP : temp = ((unsigned int) v_stack[v_top - 1]) << ((unsigned int) v_stack[v_top]);
- X v_stack[v_top - 1] = (double) temp;
- X break;
- X case MUL_OP : v_stack[v_top - 1] *= v_stack[v_top];
- X break;
- X case OR_OP : temp = ((unsigned int) v_stack[v_top - 1]) | ((unsigned int) v_stack[v_top]);
- X v_stack[v_top - 1] = (double) temp;
- X break;
- X case ROL_OP : for (i = (unsigned int) v_stack[v_top], temp = (unsigned int) v_stack[v_top - 1]; i; i--)
- X temp = (temp << 1) + ((((unsigned) temp) >> (curr_width[index_of(curr_base)] - 1)) & 1);
- X v_stack[v_top - 1] = (double) low_order(curr_width[index_of(curr_base)], temp);
- X break;
- X case ROOT_OP : v_stack[v_top - 1] = pow(v_stack[v_top - 1], 1.0 / v_stack[v_top]);
- X break;
- X case ROR_OP : for (i = (unsigned int) v_stack[v_top], temp = (unsigned int) v_stack[v_top - 1]; i; i--)
- X temp = (((unsigned) temp) >> 1) + ((temp & 1) << (curr_width[index_of(curr_base)] - 1));
- X v_stack[v_top - 1] = (double) low_order(curr_width[index_of(curr_base)], temp);
- X break;
- X case RSA_OP : temp = ((unsigned int) v_stack[v_top - 1]) >> ((unsigned int) v_stack[v_top]);
- X v_stack[v_top - 1] = (double) temp;
- X break;
- X case RSL_OP : temp = ((unsigned int) ((unsigned int) v_stack[v_top - 1])) >> ((unsigned int) v_stack[v_top]);
- X v_stack[v_top - 1] = (double) temp;
- X break;
- X case SUB_OP : v_stack[v_top - 1] -= v_stack[v_top];
- X break;
- X case XOR_OP : temp = ((unsigned int) v_stack[v_top - 1]) ^ ((unsigned int) v_stack[v_top]);
- X v_stack[v_top - 1] = (double) temp;
- X break;
- X case Y2X_OP : v_stack[v_top - 1] = pow(v_stack[v_top - 1], v_stack[v_top]);
- X break;
- X }
- X v_top--;
- X}
- X
- XPRIVATE reduce_stack(pr)
- X
- Xint pr;
- X
- X{
- X while (o_top > 0 && pr <= prec(o_stack[o_top - 1]))
- X pop_op();
- X}
- X
- XPRIVATE double to_rads(val)
- X
- Xdouble val;
- X
- X{
- X if (trig_mode == DEG)
- X return(val / 180.0 * PI);
- X else if (trig_mode == GRAD)
- X return(val / 200.0 * PI);
- X else
- X return(val);
- X}
- X
- XPRIVATE double from_rads(val)
- X
- Xdouble val;
- X
- X{
- X if (trig_mode == DEG)
- X return(val / PI * 180.0);
- X else if (trig_mode == GRAD)
- X return(val / PI * 200.0);
- X else
- X return(val);
- X}
- X
- XPUBLIC char *op_str(op)
- X
- Xint op;
- X
- X{
- X switch (op) {
- X case ADD_OP : return("+");
- X case AND_OP : return("&");
- X case DIV_OP : return("\205");
- X case LPAREN_OP : return("(");
- X case LSL_OP : return("\200");
- X case MUL_OP : return("X");
- X case OR_OP : return("|");
- X case ROL_OP : return("\202");
- X case ROOT_OP : return("\207\210");
- X case ROR_OP : return("\203");
- X case RSA_OP : return("\204");
- X case RSL_OP : return("\201");
- X case SUB_OP : return("-");
- X case XOR_OP : return("^");
- X case Y2X_OP : return("\206");
- X default : return("");
- X }
- X}
- X
- XPUBLIC do_unary(op)
- X
- Xint op;
- X
- X{ int temp;
- X
- X convert_display();
- X switch (op) {
- X case CLEAR_OP : v_stack[v_top = 0] = 0.0;
- X o_top = 0;
- X break;
- X case ERASE_OP : clear_entry();
- X do_digit(DIGIT_0);
- X break;
- X case EQUAL_OP : reduce_stack(-1);
- X break;
- X case LPAREN_OP : o_stack[o_top++] = LPAREN_OP;
- X break;
- X case RPAREN_OP : reduce_stack(prec(op));
- X o_top--;
- X break;
- X case INVEE_OP : ee_mode = FALSE;
- X break;
- X case COS_OP : v_stack[v_top] = cos(to_rads(v_stack[v_top]));
- X break;
- X case EXP_OP : v_stack[v_top] = exp(v_stack[v_top]);
- X break;
- X case E_OP : v_stack[v_top] = E;
- X break;
- X case FACT_OP : temp = (int) v_stack[v_top];
- X if (temp < 0 || temp > 170)
- X v_stack[v_top] /= 0.0;
- X else if (temp == 0)
- X v_stack[v_top] = 1.0;
- X else
- X for (v_stack[v_top] = 1.0; temp; temp--)
- X v_stack[v_top] *= (double) temp;
- X break;
- X case ICOS_OP : v_stack[v_top] = from_rads(acos(v_stack[v_top]));
- X break;
- X case INT_OP : v_stack[v_top] = (double) ((int) v_stack[v_top]);
- X break;
- X case ISIN_OP : v_stack[v_top] = from_rads(asin(v_stack[v_top]));
- X break;
- X case ITAN_OP : v_stack[v_top] = from_rads(atan(v_stack[v_top]));
- X break;
- X case LN_OP : v_stack[v_top] = log(v_stack[v_top]);
- X break;
- X case LOG_OP : v_stack[v_top] = log10(v_stack[v_top]);
- X break;
- X case NOT_OP : v_stack[v_top] = (double) low_order(curr_width[index_of(curr_base)], (~ (unsigned int) v_stack[v_top]));
- X break;
- X case OVER_OP : v_stack[v_top] = 1.0 / v_stack[v_top];
- X break;
- X case PI_OP : v_stack[v_top] = PI;
- X break;
- X case POW_OP : v_stack[v_top] = pow(10.0, v_stack[v_top]);
- X break;
- X case SIN_OP : v_stack[v_top] = sin(to_rads(v_stack[v_top]));
- X break;
- X case SQRT_OP : v_stack[v_top] = sqrt(v_stack[v_top]);
- X break;
- X case SQR_OP : v_stack[v_top] *= v_stack[v_top];
- X break;
- X case TAN_OP : v_stack[v_top] = tan(to_rads(v_stack[v_top]));
- X break;
- X case TRUNC_OP : if (curr_width[0] != -1) {
- X v_stack[v_top] *= pow(10.0, (double) curr_width[0]);
- X v_stack[v_top] = (double) ((int) v_stack[v_top]);
- X v_stack[v_top] /= pow(10.0, (double) curr_width[0]);
- X }
- X break;
- X }
- X update_display();
- X}
- X
- XPUBLIC do_binary(op)
- X
- Xint op;
- X
- X{
- X convert_display();
- X reduce_stack(prec(op));
- X o_stack[o_top++] = op;
- X v_top += 1;
- X v_stack[v_top] = v_stack[v_top - 1];
- X update_display();
- X}
- SHAR_EOF
- if test 8116 -ne "`wc -c < 'ops.c'`"
- then
- echo shar: error transmitting "'ops.c'" '(should have been 8116 characters)'
- fi
- fi # end of overwriting check
- echo shar: extracting "'screen.c'" '(16521 characters)'
- if test -f 'screen.c'
- then
- echo shar: will not over-write existing file "'screen.c'"
- else
- sed 's/^ X//' << \SHAR_EOF > 'screen.c'
- X/************************************************************************/
- X/* Copyright 1988 by Chuck Musciano and Harris Corporation */
- X/* */
- X/* Permission to use, copy, modify, and distribute this software */
- X/* and its documentation for any purpose and without fee is */
- X/* hereby granted, provided that the above copyright notice */
- X/* appear in all copies and that both that copyright notice and */
- X/* this permission notice appear in supporting documentation, and */
- X/* that the name of Chuck Musciano and Harris Corporation not be */
- X/* used in advertising or publicity pertaining to distribution */
- X/* of the software without specific, written prior permission. */
- X/* Chuck Musciano and Harris Corporation make no representations */
- X/* about the suitability of this software for any purpose. It is */
- X/* provided "as is" without express or implied warranty. */
- X/************************************************************************/
- X
- X/************************************************************************/
- X/* */
- X/* Module: screen.c */
- X/* */
- X/* Function: Create calculator display */
- X/* */
- X/* Public Names: create_screen create the screen */
- X/* invert_proc invert the keyboard */
- X/* blink flash a key */
- X/* main entry point */
- X/* */
- X/* Change History: 11 Nov 86 Creation */
- X/* */
- X/************************************************************************/
- X
- X#include <stdio.h>
- X
- X#include <suntool/sunview.h>
- X#include <suntool/panel.h>
- X#include <suntool/canvas.h>
- X
- X#include "manifest.h"
- X#include "globals.h"
- X#include "keys.h"
- X
- X#define CLOSE_KEY key[0]
- X#define INVERSE_KEY key[1]
- X#define CLEAR_KEY key[2]
- X#define FIRST_KEY 3
- X#define LAST_KEY 37
- X
- X#define k(s1, s2, s3, s4, o1, o2, o3, o4, p) {{s1, s2, s3, s4}, {o1, o2, o3, o4}, {NULL, NULL, NULL, NULL}, NULL, p}
- X#define valid_key(x) ((x) > DIGIT_F || (x) < curr_base)
- X
- XPRIVATE close_proc();
- XPRIVATE event_proc();
- XPRIVATE key_proc();
- XPRIVATE radix_proc();
- XPRIVATE kb_proc();
- X
- XPUBLIC store_proc();
- XPUBLIC recall_proc();
- XPUBLIC exchange_proc();
- XPUBLIC fix_proc();
- XPUBLIC invert_proc();
- X
- Xtypedef struct key_rec *key_ptr;
- X
- Xstruct key_rec {char *label[4];
- X int opcode[4];
- X struct pixrect *image[4];
- X Panel_item item;
- X int (*proc)();
- X };
- X
- XFrame bf;
- XPanel keys, sw;
- XCanvas display;
- Xstruct pixfont *key_font;
- X
- XPRIVATE short mask_bits[] = {0xaaaa, 0x5555};
- Xmpr_static(mask, 16, 2, 1, mask_bits);
- X
- XPRIVATE struct key_rec key[] = {
- X k( "Close", "Quit", "Close", "Quit", CLOSE_OP, QUIT_OP, CLOSE_OP, QUIT_OP, close_proc),
- X k( "Inverse", "Inverse", "Inverse", "Inverse", INVERT_OP, INVERT_OP, INVERT_OP, INVERT_OP, invert_proc),
- X k( "Erase", "Clear", "Erase", "Clear", ERASE_OP, CLEAR_OP, ERASE_OP, CLEAR_OP, key_proc),
- X k( "Sto", "", "Sto", "", STO_OP, NO_OP, STO_OP, NO_OP, store_proc),
- X k( "Rcl", "", "Rcl", "", RCL_OP, NO_OP, RCL_OP, NO_OP, recall_proc),
- X k( "Exc", "", "Exc", "", EXC_OP, NO_OP, EXC_OP, NO_OP, exchange_proc),
- X k( "(", "", "(", "", LPAREN_OP, NO_OP, LPAREN_OP, NO_OP, key_proc),
- X k( ")", "", ")", "", RPAREN_OP, NO_OP, RPAREN_OP, NO_OP, key_proc),
- X k( "EE", "EE", "", "", DIGIT_EE, INVEE_OP, NO_OP, NO_OP, key_proc),
- X k( "X", "", "X", "", MUL_OP, NO_OP, MUL_OP, NO_OP, key_proc),
- X k( "log", "10\200", "D", "", LOG_OP, POW_OP, DIGIT_D, NO_OP, key_proc),
- X k( "ln", "e\200", "E", "", LN_OP, EXP_OP, DIGIT_E, NO_OP, key_proc),
- X k( "y\200", "\203\204", "F", "", Y2X_OP, ROOT_OP, DIGIT_F, NO_OP, key_proc),
- X k( "7", "", "7", "", DIGIT_7, NO_OP, DIGIT_7, NO_OP, key_proc),
- X k( "8", "", "8", "", DIGIT_8, NO_OP, DIGIT_8, NO_OP, key_proc),
- X k( "9", "", "9", "", DIGIT_9, NO_OP, DIGIT_9, NO_OP, key_proc),
- X k( "\214", "", "\214", "", DIV_OP, NO_OP, DIV_OP, NO_OP, key_proc),
- X k( "sin", "sn\205", "A", "", SIN_OP, ISIN_OP, DIGIT_A, NO_OP, key_proc),
- X k( "cos", "cs\205", "B", "", COS_OP, ICOS_OP, DIGIT_B, NO_OP, key_proc),
- X k( "tan", "tn\205", "C", "", TAN_OP, ITAN_OP, DIGIT_C, NO_OP, key_proc),
- X k( "4", "", "4", "", DIGIT_4, NO_OP, DIGIT_4, NO_OP, key_proc),
- X k( "5", "", "5", "", DIGIT_5, NO_OP, DIGIT_5, NO_OP, key_proc),
- X k( "6", "", "6", "", DIGIT_6, NO_OP, DIGIT_6, NO_OP, key_proc),
- X k( "+", "", "+", "", ADD_OP, NO_OP, ADD_OP, NO_OP, key_proc),
- X k( "\202\201", "x\206", "<<", "\217\220", SQRT_OP, SQR_OP, LSL_OP, ROL_OP, key_proc),
- X k( "\210\211", "", "Not", "\221>>", OVER_OP, NO_OP, NOT_OP, RSA_OP, key_proc),
- X k( "x!", "", ">>", "\215\216", FACT_OP, NO_OP, RSL_OP, ROR_OP, key_proc),
- X k( "1", "", "1", "", DIGIT_1, NO_OP, DIGIT_1, NO_OP, key_proc),
- X k( "2", "", "2", "", DIGIT_2, NO_OP, DIGIT_2, NO_OP, key_proc),
- X k( "3", "", "3", "", DIGIT_3, NO_OP, DIGIT_3, NO_OP, key_proc),
- X k( "-", "", "-", "", SUB_OP, NO_OP, SUB_OP, NO_OP, key_proc),
- X k( "\207", "e", "And", "", PI_OP, E_OP, AND_OP, NO_OP, key_proc),
- X k( "Int", "Trc", "Or", "Xor", INT_OP, TRUNC_OP, OR_OP, XOR_OP, key_proc),
- X k( "Fix", "DRG", "Fix", "", FIX_OP, FIX_OP, FIX_OP, NO_OP, fix_proc),
- X k( ".", "", "", "", DIGIT_DOT, NO_OP, NO_OP, NO_OP, key_proc),
- X k( "0", "", "0", "", DIGIT_0, NO_OP, DIGIT_0, NO_OP, key_proc),
- X k( "\212\213", "", "\212\213", "", DIGIT_CHS, NO_OP, DIGIT_CHS, NO_OP, key_proc),
- X k( "=", "", "=", "", EQUAL_OP, NO_OP, EQUAL_OP, NO_OP, key_proc)
- X };
- X
- Xstatic short ct_icon_image[] = {
- X#include "calc.icon"
- X};
- Xmpr_static(ct_icon_pixrect, 47, 64, 1, ct_icon_image);
- X
- XPUBLIC create_screen(argc, argv)
- X
- Xint argc;
- Xchar **argv;
- X
- X{ int i, j;
- X Icon icon;
- X
- X icon = icon_create(ICON_IMAGE, &ct_icon_pixrect, ICON_LABEL, NULL, ICON_WIDTH, 47, ICON_HEIGHT, 64, 0);
- X bf = window_create(NULL, FRAME,
- X FRAME_ARGS, argc, argv,
- X FRAME_LABEL, "<< Calctool >>",
- X FRAME_ICON, icon,
- X FRAME_SUBWINDOWS_ADJUSTABLE, FALSE,
- X FRAME_NO_CONFIRM, TRUE,
- X 0);
- X sw = window_create(bf, PANEL, 0);
- X display = window_create(bf, CANVAS, 0);
- X keys = window_create(bf, PANEL,
- X WIN_WIDTH, 360,
- X PANEL_ACCEPT_KEYSTROKE, TRUE,
- X PANEL_BACKGROUND_PROC, kb_proc,
- X 0);
- X
- X if ((key_font = pf_open(KEY_FONT)) == NULL) {
- X fprintf(stderr, "calctool: could not open font %s\n", KEY_FONT);
- X exit(1);
- X }
- X for (i = 0; i < FIRST_KEY; i++)
- X for (j = 0; j < 4; j++)
- X key[i].image[j] = panel_button_image((i == 0)? sw : keys, key[i].label[j], 7, key_font);
- X for (i = FIRST_KEY; i <= LAST_KEY; i++)
- X for (j = 0; j < 4; j += 2)
- X if (key[i].opcode[j] != NO_OP) {
- X key[i].image[j] = panel_button_image(sw, key[i].label[j], 3, key_font);
- X if (key[i].opcode[j + 1] == NO_OP) {
- X key[i].image[j + 1] = panel_button_image(sw, key[i].label[j], 3, key_font);
- X pr_replrop(key[i].image[j+1], 0, 0, key[i].image[j+1]->pr_width, key[i].image[j+1]->pr_height,
- X PIX_SRC & PIX_DST, &mask, 0, 0);
- X }
- X else
- X key[i].image[j+1] = panel_button_image(sw, key[i].label[j+1], 3, key_font);
- X }
- X
- X CLOSE_KEY.item = panel_create_item(sw, PANEL_BUTTON,
- X PANEL_LABEL_IMAGE, CLOSE_KEY.image[0],
- X PANEL_NOTIFY_PROC, CLOSE_KEY.proc,
- X PANEL_EVENT_PROC, event_proc,
- X PANEL_CLIENT_DATA, &(CLOSE_KEY),
- X 0);
- X window_fit(sw);
- X
- X INVERSE_KEY.item = panel_create_item(keys, PANEL_BUTTON,
- X PANEL_LABEL_IMAGE, INVERSE_KEY.image[0],
- X PANEL_NOTIFY_PROC, INVERSE_KEY.proc,
- X PANEL_EVENT_PROC, event_proc,
- X PANEL_CLIENT_DATA, &(INVERSE_KEY),
- X 0);
- X key[FIRST_KEY].item = panel_create_item(keys, PANEL_BUTTON,
- X PANEL_LABEL_IMAGE, key[FIRST_KEY].image[0],
- X PANEL_NOTIFY_PROC, key[FIRST_KEY].proc,
- X PANEL_EVENT_PROC, event_proc,
- X PANEL_ACCEPT_KEYSTROKE, TRUE,
- X PANEL_ITEM_X, panel_get(INVERSE_KEY.item, PANEL_ITEM_X),
- X PANEL_ITEM_Y, panel_get(INVERSE_KEY.item, PANEL_ITEM_Y) +
- X INVERSE_KEY.image[0]->pr_height + 4,
- X PANEL_CLIENT_DATA, &(key[FIRST_KEY]),
- X 0);
- X for (i = FIRST_KEY + 1; i <= LAST_KEY; i++)
- X key[i].item = panel_create_item(keys, PANEL_BUTTON,
- X PANEL_LABEL_IMAGE, key[i].image[0],
- X PANEL_NOTIFY_PROC, key[i].proc,
- X PANEL_EVENT_PROC, event_proc,
- X PANEL_ACCEPT_KEYSTROKE, TRUE,
- X PANEL_CLIENT_DATA, &(key[i]),
- X 0);
- X CLEAR_KEY.item = panel_create_item(keys, PANEL_BUTTON,
- X PANEL_LABEL_IMAGE, CLEAR_KEY.image[0],
- X PANEL_NOTIFY_PROC, CLEAR_KEY.proc,
- X PANEL_EVENT_PROC, event_proc,
- X PANEL_ITEM_X, panel_get(key[LAST_KEY].item, PANEL_ITEM_X) +
- X key[LAST_KEY].image[0]->pr_width - CLEAR_KEY.image[0]->pr_width,
- X PANEL_ITEM_Y, panel_get(INVERSE_KEY.item, PANEL_ITEM_Y),
- X PANEL_CLIENT_DATA, &(CLEAR_KEY),
- X 0);
- X panel_create_item(keys, PANEL_CHOICE,
- X PANEL_LABEL_STRING, "",
- X PANEL_DISPLAY_LEVEL, PANEL_CURRENT,
- X PANEL_CHOICE_IMAGES, panel_button_image(keys, "Scientific", 13, key_font),
- X panel_button_image(keys, "Engineering", 13, key_font),
- X panel_button_image(keys, "Binary", 13, key_font),
- X panel_button_image(keys, "Octal", 13, key_font),
- X panel_button_image(keys, "Hexadecimal", 13, key_font),
- X 0,
- X PANEL_CHOICE_XS, panel_get(key[FIRST_KEY + 2].item, PANEL_ITEM_X), 0,
- X PANEL_CHOICE_YS, panel_get(INVERSE_KEY.item, PANEL_ITEM_Y), 0,
- X/* PANEL_CHOICE_FONTS, key_font, 0,*/
- X PANEL_NOTIFY_PROC, radix_proc,
- X 0);
- X window_fit(keys);
- X
- X window_set(sw,
- X WIN_X, 0,
- X WIN_Y, 0,
- X 0);
- X window_set(display,
- X WIN_RIGHT_OF, sw,
- X WIN_Y, 0,
- X WIN_WIDTH, window_get(keys, WIN_WIDTH) - window_get(sw, WIN_WIDTH) - 5,
- X WIN_HEIGHT, window_get(sw, WIN_HEIGHT),
- X 0);
- X window_set(keys,
- X WIN_X, 0,
- X WIN_BELOW, sw,
- X 0);
- X window_fit(bf);
- X window_set(sw,
- X WIN_WIDTH, window_get(sw, WIN_WIDTH),
- X WIN_HEIGHT, window_get(sw, WIN_HEIGHT),
- X 0);
- X window_set(display,
- X WIN_WIDTH, window_get(display, WIN_WIDTH),
- X WIN_HEIGHT, window_get(display, WIN_HEIGHT),
- X 0);
- X window_set(keys,
- X WIN_WIDTH, window_get(keys, WIN_WIDTH),
- X WIN_HEIGHT, window_get(keys, WIN_HEIGHT),
- X 0);
- X update_display();
- X window_main_loop(bf);
- X}
- X
- XPRIVATE close_proc()
- X
- X{
- X if (inverted ^ temp_inverted) {
- X window_destroy(bf);
- X exit(0);
- X }
- X else {
- X window_set(bf, FRAME_CLOSED, TRUE, 0);
- X temp_inverted = FALSE;
- X if (inverted)
- X invert_proc();
- X }
- X}
- X
- XPUBLIC invert_proc()
- X
- X{ int i;
- X
- X temp_inverted = FALSE;
- X inverted = !inverted;
- X for (i = 0; i <= LAST_KEY; i++)
- X if (key[i].image[curr_mode + (inverted? 1 : 0)] && valid_key(key[i].opcode[curr_mode]))
- X panel_set(key[i].item, PANEL_LABEL_IMAGE, key[i].image[curr_mode + (inverted? 1 : 0)], PANEL_SHOW_ITEM, TRUE, 0);
- X else
- X panel_set(key[i].item, PANEL_SHOW_ITEM, FALSE, 0);
- X}
- X
- XPRIVATE radix_proc(item, value, event)
- X
- XPanel_item item;
- Xint value;
- XEvent *event;
- X
- X{ int i;
- X static int bases[] = {DECIMAL, DECIMAL, BINARY, OCTAL, HEXADECIMAL};
- X
- X convert_display();
- X if (value == 0) {
- X curr_mode = SCIENTIFIC;
- X eng_mode = FALSE;
- X }
- X else if (value == 1) {
- X curr_mode = SCIENTIFIC;
- X eng_mode = TRUE;
- X }
- X else
- X curr_mode = PROGRAMMER;
- X curr_base = bases[value];
- X inverted = temp_inverted = FALSE;
- X for (i = FIRST_KEY; i <= LAST_KEY; i++)
- X if (key[i].opcode[curr_mode] != NO_OP && valid_key(key[i].opcode[curr_mode]))
- X panel_set(key[i].item, PANEL_LABEL_IMAGE, key[i].image[curr_mode], PANEL_SHOW_ITEM, TRUE, 0);
- X else
- X panel_set(key[i].item, PANEL_SHOW_ITEM, FALSE, 0);
- X update_display();
- X}
- X
- XPRIVATE event_proc(item, event)
- X
- XPanel_item item;
- XEvent *event;
- X
- X{ key_ptr ky;
- X static key_ptr curr_key;
- X int op;
- X
- X if (event_id(event) >= ASCII_FIRST && event_id(event) <= ASCII_LAST) {
- X keyboard(event_id(event));
- X return;
- X }
- X ky = (key_ptr) panel_get(item, PANEL_CLIENT_DATA);
- X switch (event_id(event)) {
- X case MS_LEFT : if (ky->opcode[curr_mode + (inverted? 1 : 0)] != NO_OP)
- X if (event_is_down(event)) {
- X panel_begin_preview(item, event);
- X curr_key = ky;
- X }
- X else if (curr_key == ky) {
- X panel_accept_preview(item, event);
- X curr_key = NULL;
- X }
- X break;
- X case MS_MIDDLE : if (ky->opcode[curr_mode + (inverted? 0 : 1)] != NO_OP)
- X if (event_is_down(event)) {
- X panel_set(item, PANEL_LABEL_IMAGE, ky->image[curr_mode + (inverted? 0 : 1)], 0);
- X panel_begin_preview(item, event);
- X curr_key = ky;
- X }
- X else if (curr_key == ky) {
- X temp_inverted = TRUE;
- X panel_accept_preview(item, event);
- X panel_set(item, PANEL_LABEL_IMAGE, ky->image[curr_mode + (inverted? 1 : 0)], 0);
- X curr_key = NULL;
- X }
- X break;
- X case MS_RIGHT : if (event_is_down(event))
- X if ((op = ky->opcode[curr_mode + (inverted? 1 : 0)]) == STO_OP ||
- X op == RCL_OP ||
- X op == EXC_OP ||
- X op == FIX_OP) {
- X panel_begin_preview(item, event);
- X panel_accept_preview(item, event);
- X }
- X break;
- X case PANEL_EVENT_CANCEL : if (curr_key == ky) {
- X panel_cancel_preview(item, event);
- X panel_set(item, PANEL_LABEL_IMAGE, ky->image[curr_mode + (inverted? 1 : 0)], 0);
- X curr_key = NULL;
- X }
- X break;
- X }
- X}
- X
- XPRIVATE key_proc(item, event)
- X
- XPanel_item item;
- XEvent *event;
- X
- X{ key_ptr ky;
- X int op;
- X
- X ky = (key_ptr) panel_get(item, PANEL_CLIENT_DATA);
- X op = ky->opcode[curr_mode + ((inverted? 1 : 0) ^ temp_inverted)];
- X if (is_digit(op))
- X do_digit(op);
- X else if (is_binary(op))
- X do_binary(op);
- X else
- X do_unary(op);
- X temp_inverted = FALSE;
- X if (inverted)
- X invert_proc();
- X}
- X
- XPRIVATE kb_proc(item, event)
- X
- XPanel_item item;
- XEvent *event;
- X
- X{
- X if (event_id(event) >= ASCII_FIRST && event_id(event) <= ASCII_LAST)
- X keyboard(event_id(event));
- X}
- X
- XPUBLIC int blink(op)
- X
- Xint op;
- X
- X{ int i, j;
- X struct pixrect *old, *new;
- X
- X for (i = FIRST_KEY; i <= LAST_KEY; i++)
- X if (op == key[i].opcode[curr_mode])
- X break;
- X old = (struct pixrect *) panel_get(key[i].item, PANEL_LABEL_IMAGE);
- X new = mem_create(old->pr_width, old->pr_height, 1);
- X pr_rop(new, 0, 0, new->pr_width, new->pr_height, PIX_NOT(PIX_SRC), old, 0, 0);
- X panel_set(key[i].item, PANEL_LABEL_IMAGE, new, 0);
- X for (j = 25000; j; j--)
- X ;
- X panel_set(key[i].item, PANEL_LABEL_IMAGE, old, 0);
- X pr_destroy(new);
- X return(op);
- X}
- X
- Xmain(argc, argv)
- X
- Xint argc;
- Xchar **argv;
- X
- X{
- X create_screen(argc, argv);
- X}
- SHAR_EOF
- if test 16521 -ne "`wc -c < 'screen.c'`"
- then
- echo shar: error transmitting "'screen.c'" '(should have been 16521 characters)'
- fi
- fi # end of overwriting check
- # End of shell archive
- exit 0
-